package com.example.administrator.resistancecalculator;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final float c = 200000;
    private EditText v, mv;
    private TextView tv_result, tv_showResult;
    private String pv;
    private String wv;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        v = findViewById(R.id.v);
        mv = findViewById(R.id.mv);
        tv_result = findViewById(R.id.result);
        Button btn = findViewById(R.id.count);
        listView = findViewById(R.id.combination);
        tv_showResult = findViewById(R.id.showResult);
    }

    private void getProbeVoltage() {
        pv = v.getText().toString().trim();
    }

    private void showMsg(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    }

    private void getWorkingVoltage() {
        wv = mv.getText().toString().trim();
    }

    @SuppressLint("SetTextI18n")
    public void getCount(View view) {
        getProbeVoltage();
        getWorkingVoltage();
        if (pv.isEmpty()) {
            showMsg(getResources().getString(R.string.probe_voltage));
            return;
        }
        if (wv.isEmpty()) {
            showMsg(getResources().getString(R.string.working_voltage));
            return;
        }
        hintKbTwo();
        int round = Math.round((c / ((Float.valueOf(wv) / Float.valueOf(pv)) - 5)) / 1000);
        Log.d("结果",(c / ((Float.valueOf(wv) / Float.valueOf(pv)) - 5)) / 1000+"");
        if (round > 0) {
            v.clearFocus();
            mv.clearFocus();
            tv_result.setText(String.valueOf(round) + " K");
            showResult(round);
            tv_showResult.setVisibility(View.VISIBLE);
        } else {
            tv_result.setText("非法数据,请重新输入！");
            v.setText("");
            mv.setText("");
            mv.clearFocus();
            v.requestFocus();
            showResult(0);
//            v.setFocusable(true);
//            v.setCursorVisible(true);
            tv_showResult.setVisibility(View.GONE);
        }

    }

    private void showResult(int round) {
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < round; i++) {
            int j = round - i;
            Log.d("循环结果", j + "+" + i);
            strings.add(j + " K + " + i + " K");
        }
        ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, strings);
        //实现列表的显示
        listView.setAdapter(adapter);

    }

    //此方法只是关闭软键盘
    private void hintKbTwo() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        if (imm.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
}
